package com.gxlevi.api;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.GetChildrenBuilder;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class ZookeeperAPITest {
    private CuratorFramework client;

    @Before
    public void init() {
        //1.定制一个重试策略
        /**
         * param1 :重试间隔时间
         * param3 :重试的最大次数
         */
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 1);
        //2.获取客户端对象
        /**
         * param1:要连接的zookeeper服务器列表
         * param2:会话的超时时间
         * param3:链接超时时间
         * param4:重试策略
         */
        String connectionStr = "192.168.100.101:2181,192.168.100.111:2181,192.168.100.121:2181";
        client = CuratorFrameworkFactory.newClient(connectionStr, 8000, 8000, retryPolicy);
    }

    @Test
    public void createZnode() throws Exception {
        //3.开启客户端
        client.start();
        //4.创建永久性节点
        client.create()
                .creatingParentsIfNeeded()//可以创建层级节点
                .withMode(CreateMode.PERSISTENT)
                .forPath("/hello/abc", "worlds".getBytes());

    }

    @Test
    public void createTmpZnode() throws Exception {
        //3.开启客户端
        client.start();
        //4.创建临时节点
        client.create()
                .creatingParentsIfNeeded()//可以创建层级节点
                .withMode(CreateMode.EPHEMERAL)
                .forPath("/hello4", "worlds".getBytes());
        Thread.sleep(5000);
    }

    @Test
    public void setZnodeData() throws Exception {
        client.start();
        //修改节点数据
        client.setData().forPath("/hello", "zookeeper".getBytes());
    }

    @Test
    public void getZnodeData() throws Exception {
        client.start();
        //获取节点数据
        byte[] bytes = client.getData().forPath("/hello");
        System.out.println(new String(bytes));
    }

    @Test
    public void deleteZnode() throws Exception {
        client.start();
        client.delete().deletingChildrenIfNeeded().forPath("/hello");
    }

    @Test
    public void watchZnode() throws Exception {
        client.start();
        //创建一个TreeCache对象，指定要监控的节点路径
        TreeCache treeCache = new TreeCache(client, "/hello3");
        //自定义一个监听器
        treeCache.getListenable().addListener(new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
                ChildData data = treeCacheEvent.getData();
                if (data != null) {
                    switch (treeCacheEvent.getType()) {
                        case NODE_ADDED:
                            System.out.println("监控到有新增节点");
                            break;
                        case NODE_REMOVED:
                            System.out.println("有节点被移除");
                            break;
                        case NODE_UPDATED:
                            System.out.println("监控到节点被更新");
                            break;
                        default:
                            break;
                    }
                }
            }
        });

        //开始监听
        treeCache.start();

        Thread.sleep(60000000);
    }

    @After
    public void close() {
        //5.关闭客户端
        client.close();
    }
}
